package com.tengits.mqttmonitor;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.facebook.react.ReactActivity;
import com.tencent.mars.xlog.Log;
import com.tengits.mqttmonitor.rnlibs.mqttbroker.NetworkChangeReceiver;
import org.devio.rn.splashscreen.SplashScreen;

public class MainActivity extends ReactActivity {

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "MQTTMonitor";
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.show(this, true);  // here
    super.onCreate(savedInstanceState);
    NetworkChangeReceiver.registerReceiver(this);
    try{
      //需要的权限
      String[] permArr={
              Manifest.permission.READ_EXTERNAL_STORAGE,
              Manifest.permission.WRITE_EXTERNAL_STORAGE,
      };
      boolean needReq = false;
      for(int i=0;i<permArr.length;i++){
        if(ContextCompat.checkSelfPermission(this, permArr[i])!= PackageManager.PERMISSION_GRANTED){
          needReq = true;
          break;
        }
      }
      if(needReq)
        ActivityCompat.requestPermissions(this,permArr,1);
    }catch (Exception e) {
     e.printStackTrace();
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    NetworkChangeReceiver.unRegisterReceiver(this);
    Log.appenderClose();
  }
}